<?php
namespace App\Http\Controllers\Admin;

use App\DBs\Admin\VideoDB;
use App\Utilitys\Upload;
use Illuminate\Http\Request;

class VideoController extends BaseController
{
    public function __construct()
    {
        $this->dbVideo = new VideoDB();
    }

    //大会视频
    public function getVideoIndex()
    {
        $res = $this->dbVideo->getVideoIndex();
        if ($res['code']!=0) {
            return $this->toJson(-2,$res['msg']);
        }
        return $this->toJson(0,'success',$res['data']);
    }

    //视频上传
    public function setUpload(Request $request)
    {
//        $file = $request->file('file');
//        $fname = $file->getClientOriginalName();
//        $fsize = $file->getClientSize();
//        echo '<pre>'; var_dump($file,$fname,$fsize); exit;
        if (!isset($_FILES['file']) || !$_FILES['file']) {
            echo '<script>alert(\'没有上传\');history.go(-1);</script>'; exit;
        }
//        echo '<pre>'; var_dump($_FILES); exit;
        $file = Upload::uploadFile($request);
        if ($file['code']!=0) {
            echo '<script>alert(\''.$file['msg'].'\');history.go(-1);</script>'; exit;
        }
//        echo '<pre>'; var_dump($file); exit;
        $code = $this->dbVideo->setUpload($file['data']);
        if (!$code) {
            echo '<script>alert(\'上传错误\');history.go(-1);</script>'; exit;
        }
        return redirect('/admin/videoAdd.html?vcode='.$code.'&link='.$file['data']);
    }

    //视频新增
    public function setVideoAdd(Request $request)
    {
        $arys = $request->all();
        $title = isset($arys['title'])&&$arys['title'] ? $arys['title'] : '';
        $img = isset($arys['img'])&&$arys['img'] ? $arys['img'] : '';
        $link = isset($arys['link'])&&$arys['link'] ? $arys['link'] : '';
        $vcode = isset($arys['vcode'])&&$arys['vcode'] ? $arys['vcode'] : '';
        if (!$img) {
            return $this->toJson(-2,'没有图片上传');
        }
        $upData = [
            'title'=>$title, 'link'=>$link, 'vcode'=>$vcode,
        ];
        if ($img) $upData['img'] = Upload::getPathByBase64($img);
        $res = $this->dbVideo->setVideoAdd($upData);
        if ($res['code']!=0) {
            return $this->toJson(-2,$res['msg']);
        }
        return $this->toJson(0);
    }

    //视频删除
    public function setVideoDel(Request $request)
    {
        $arys = $request->all();
        $id = isset($arys['id'])&&$arys['id'] ? $arys['id'] : 0;
        $res = $this->dbVideo->setVideoDel($id);
        if ($res['code']!=0) {
            return $this->toJson(-2,$res['msg']);
        }
        return $this->toJson(0);
    }
}